<?php

class Adept_Config_Ini {

    protected $defaultSection = 'default';
    protected $filename = '';
    protected $values;

    public function __construct($filename = '') {
        $this->values = array();
        if (!empty($filename)) {
            $this->load($filename);
        }
    }

    public function clear() {
        $this->values = array();
    }

    public function load($filename) {
        if (!file_exists($filename)) return false;

        $lines = @file($filename);

        $this->filename = $filename;
        $currentSection = $this->defaultSection;

        foreach ($lines as $line) {
            if (trim($line) == '') {
                continue;
            }

            // removing comments after #, not after # inside ""
            $line = preg_replace('/([^"#]+|"(.*?)")|(#[^#]*)/', "\\1", $line);

            // new group
            if (preg_match("#^\[(.+)\]\s*$#", $line, $newSectionNameArray)) {
                $newSectionName = trim($newSectionNameArray[1]);
                $currentSection = $newSectionName;
                $this->values[$currentSection] = array();
                continue;
            }
            // check for value
            if (preg_match("#^([a-zA-Z0-9\_\-]+)(\[([a-zA-Z0-9\_\-]*)\]){0,1}(\s*)=(.*)$#", $line, $regValues))
            {
                $varName = trim($regValues[1]);
                $varValue = trim($regValues[5]);

                // remove quotes
                if (preg_match('/^"(.*)"$/', $varValue, $m)) {
                    $varValue = $m[1];
                }

                if ($regValues[2]) {
                    // it's an array
                    if ($regValues[3]) {
                        // array key presebt
                        $key_name = $regValues[3];

                        if (isset($this->values[$currentSection][$varName]) &&
                        is_array($this->values[$currentSection][$varName])) {
                            $this->values[$currentSection][$varName][$key_name] = $varValue;
                        } else {
                            $this->values[$currentSection][$varName] = array($key_name => $varValue);
                        }
                    } else {
                        // it's an array but no index
                        // append to array
                        if (isset($this->values[$currentSection][$varName]) &&
                        is_array($this->values[$currentSection][$varName]))
                        $this->values[$currentSection][$varName][] = $varValue;
                        else {
                            $this->values[$currentSection][$varName] = array($varValue);
                        }
                    }
                } else {
                    // it's not an array
                    $this->values[$currentSection][$varName] = $varValue;
                }
            }
        }
    }

    /**
   * Return all values in array.
   *
   * @return array
   */
    public function getAll() {
        return $this->values;
    }

    public function getValuesList() {

        $result = array();
        foreach ($this->values as $groupId => $group) {
            foreach ($group as $key => $value) {
                if ($groupId != $this->defaultSection) {
                    $key = $groupId . ":" . $key;
                }
                $result[$key] = $value;
            }
        }
        return $result;
    }

    public function getSection($sectionName) {
        if (isset($this->values[$sectionName])) {
            return $this->values[$sectionName];
        } else {
            return false;
        }
    }

    public function hasSection($section) {
        return isset($this->values[$section]);
    }

    /**
   * Try to get option in file. If option exists return 'true',
   * otherwise return 'false'.
   *
   * @param string $key
   * @param string $section
   * @return string
   */
    public function hasValue($key, $section = '') {
        if (!$section) $section = $this->defaultSection;
        return isset($this->values[$section][$key]);
    }

    /**
   * Return option value. If option not exists, return false
   *
   * @param string $key Key value
   * @param string $section Section value, by default used $this->defaultSection
   * @return string 
   */
    public function getOption($key, $section = '') {
        if (!$section) {
            $section = $this->defaultSection;
        }
        if (isset($this->values[$section][$key])) {
            return $this->values[$section][$key];
        } else {
            return false;
        }
    }

}
